package com.kedacom.demo.healthcheck;

import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * Mysql健康检查
 * @author luocanfeng
 * @date 2016年9月2日
 */
public class MysqlHealthCheckItem extends HealthCheckItem {

	/** 待检查的数据源 */
	private DataSource dataSource;

	{
		type = Type.MySQL;
	}

	public MysqlHealthCheckItem() {
		type = Type.MySQL;
	}

	public MysqlHealthCheckItem(String name) {
		this();
		this.name = name;
	}

	public MysqlHealthCheckItem(String name, DataSource dataSource) {
		this(name);
		this.dataSource = dataSource;
	}

	/**
	 * 检查Mysql数据库连接是否正常
	 */
	@Override
	public boolean doCheck() {
		try {
			return dataSource.getConnection().isValid(3);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

}
